// [[ OnflowGenerator File UUID: bd653342191f44a9b3b29930f8f4a28c  ]]
package com.itdct.onflow.system.dict.manager

import com.alibaba.fastjson2.toJSONString
import com.itdct.onflow.core.extend.logInfo
import com.itdct.onflow.core.extend.logWarn
import com.itdct.onflow.core.extend.selectAll
import com.itdct.onflow.system.common.base.BaseManager
import com.itdct.onflow.system.dict.dto.DictDTO
import com.itdct.onflow.system.dict.global.DictMapGlobal.dictCodeEntityMap
import com.itdct.onflow.system.dict.global.DictMapGlobal.dictCodeNameMap
import com.itdct.onflow.system.dict.mapper.SysDictMapper
import org.springframework.stereotype.Component

/**
 * @author DCTANT
 * @date 2025-09-01 14:43:30
 * @version 1.0
 * @description 系统字典中间层
 */
@Component
class SysDictManager(
    val sysDictMapper: SysDictMapper
) : BaseManager() {

    @Synchronized
    fun initDict() {
        logInfo("开始初始化字典")
        val dictDTOs = ArrayList<DictDTO>()
        val dtoMap = HashMap<Long, DictDTO>()

        val dictEntities = sysDictMapper.selectAll()
        val idEntityMap = dictEntities.associateBy { it.id!! }
        for (dictEntity in dictEntities) {
            val id = dictEntity.id!!
            val parentId = dictEntity.parentId

            val dictDTO = DictDTO()
            dictDTO.dictEntity = dictEntity
            if (parentId == 0L) {
                dictEntity.codePath = dictEntity.code
                dictDTOs.add(dictDTO)
                dtoMap[id] = dictDTO
            } else {
                val parentDictEntity = idEntityMap[parentId]
                parentDictEntity?.also {
                    dictDTO.parentEntity = parentDictEntity
                    dictEntity.codePath = "${parentDictEntity.codePath}.${dictEntity.code}"
                } ?: also {
                    logWarn("无法找到字典ID：${dictEntity.id}，对应的父类ID：$parentId")
                }

                dtoMap[parentId]?.also {
                    it.children.add(dictDTO)
                }
            }
        }
        dictCodeEntityMap.clear()
        dictCodeNameMap.clear()

        refreshDictMap(dictDTOs, "")

//        dictMapper.saveBatch(dictEntities)

        logInfo("字典初始化完成，值为：${dictCodeNameMap.toJSONString()}")

    }

    private fun refreshDictMap(dictDtos: ArrayList<DictDTO>, parentCode: String) {
        for (dictDto in dictDtos) {
            val dictEntity = dictDto.dictEntity
            val code = dictEntity.code
            if (parentCode.isBlank()) {
                dictCodeNameMap[code] = dictEntity.name
                dictCodeEntityMap[code] = dictEntity
                if (dictDto.children.isNotEmpty()) {
                    refreshDictMap(dictDto.children, code)
                }
            } else {
                val nowCode = "${parentCode}/${dictEntity.code}"
                dictCodeNameMap[nowCode] = dictEntity.name
                dictCodeEntityMap[nowCode] = dictEntity
                if (dictDto.children.isNotEmpty()) {
                    refreshDictMap(dictDto.children, nowCode)
                }
            }
        }
    }

}